package cm.mefobe.android.telecomcost.entities;

import java.io.InvalidObjectException;
import java.util.ArrayList;
import android.text.format.*;

public class MonthlyThreshold extends DayRangeThreshold {

	private Month _month;

	public CharSequence getMonthNameLong() {
		return DateUtils.getMonthString(_month.getMonthOfYear(),
				DateUtils.LENGTH_LONG);
	}

	public CharSequence getMonthNameShort() {
		return DateUtils.getMonthString(_month.getMonthOfYear(),
				DateUtils.LENGTH_SHORT);
	}

	public MonthlyThreshold(double d, Month monthOfYear) {
		super(d, new DayRange(monthOfYear.getFirstDay(), monthOfYear.getLastDay()));
		setMonthOfYear(monthOfYear);
	}

	public MonthlyThreshold() {
	}

	@Override
	public String getType() {
		return "MonthlyThreshold";
	}

	@Override
	public void setType(String type) {

	}

	public void setMonthOfYear(Month monthOfYear) {
		this._month = monthOfYear;
	}

	public Month getMonth() {
		return _month;
	}

	@Override
	public String toString() {
		return _month.toString() + " ( " + getAmount() + " )";
	}

	@Override
	public int hashCode() {
		return _month.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		MonthlyThreshold other = (MonthlyThreshold) obj;
		if (_month == null) {
			if (other._month != null)
				return false;
		} else if (!_month.equals(other._month))
			return false;
		return true;
	}

	public static final String PROP_TYPE = "MonthlyThreshold";

	public String[] getWeeksArrayAdapter() {
		int index = 0;
		String[] thresholds = new String[getWeeklyThresholds().size()];
		while (index < getWeeklyThresholds().size()) {
			thresholds[index] = getWeeklyThresholds().toString();
			index++;
		}
		return thresholds;
	}

	public ArrayList<WeeklyThreshold> getWeeklyThresholds() {
		ArrayList<WeeklyThreshold> thresholds = new ArrayList<WeeklyThreshold>();
		try {
			ArrayList<Week> weeks = getMonth().getWeeks();

			int index = 0;
			while (index < weeks.size()) {
				DayRangeThreshold limit = new DayRangeThreshold(new DayRange(weeks.get(index).getFirstDay(),weeks.get(index).getLastDay()));
				limit.getDailyThresholds();

				WeeklyThreshold limit1 = new WeeklyThreshold(
						limit.getCalculatedAmount(), weeks.get(index));
				thresholds.add(limit1);

				index++;
			}
		} catch (InvalidObjectException e) {
			e.printStackTrace();
		}
		return thresholds;
	}
}
